/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.tukano.swingdeco.draganddrop;

import java.awt.Component;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;

/**
 *
 * @author Pietro
 */
public abstract class DropHandler<T> {

    private final ItemTransformer<T> transformer;

    public DropHandler(ItemTransformer<T> transformer) {
        this.transformer = transformer;
    }

    public abstract void onDrop(Component target, T droppedItem);

    public void install(final Component dropper) {
        DropTarget dt = new DropTarget(dropper, new DropTargetAdapter() {

            public void drop(DropTargetDropEvent e) {
                T item = transformer.transformEvent(e);
                if(item != null) {
                    onDrop(dropper, item);
                }
            }
        });
    }
}
